package com.lingshu.app.ui.login;

import androidx.lifecycle.MutableLiveData;

import com.lingshu.app.base.BaseViewModel;
import com.lingshu.app.model.auth.LoginBean;
import com.lingshu.app.network.RetrofitAuthClient;
import com.lingshu.app.network.CustomDisposable;
import com.lingshu.app.repository.UserRepository;
import com.lingshu.app.service.AuthService;

import javax.inject.Inject;

import dagger.hilt.android.lifecycle.HiltViewModel;

/**
 * @author 于定金
 * @version 1.0
 * @Date 2024/1/3 11:20
 * @Description
 */
@HiltViewModel
public class LoginViewModel extends BaseViewModel {
    private MutableLiveData<LoginBean> loginInfo;

    public MutableLiveData<LoginBean> getLoginInfo() {
        if (loginInfo == null) {
            loginInfo = new MutableLiveData<>();
        }
        return loginInfo;
    }

    private final UserRepository userRepository;

    @Inject
    LoginViewModel(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public void login() {
        CustomDisposable.addDisposable(RetrofitAuthClient.getInstance().getService(AuthService.class).login(loginInfo.getValue()), returnResult -> {
            loginResult.postValue(returnResult);
        }, throwable -> {
            infoMsg.postValue(throwable.getMessage());
        });
    }

    /**
     * 数据初始化
     */
    @Override
    public void initData() {
        title.postValue("登录");
    }
}
